package cn.anecansaitin.whimsy.common.listener;

import cn.anecansaitin.whimsy.Whimsy;
import cn.anecansaitin.whimsy.common.capabilities.battle.IBattleMode;
import cn.anecansaitin.whimsy.common.capabilities.battle.IOWController;
import cn.anecansaitin.whimsy.common.event.PlayerHealingCompletedEvent;
import net.minecraft.world.entity.player.Player;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.fml.common.Mod;

@Mod.EventBusSubscriber(modid = Whimsy.MODID)
public class PlayerHeal {
    @SubscribeEvent
    public static void heal(PlayerHealingCompletedEvent event) {
        Player player = event.getEntity();

        IBattleMode mode = player.getCapability(IBattleMode.BATTLE_MODE);

        if (mode == null) {
            return;
        }

        if (!(mode.getController() instanceof IOWController controller)) {
            return;
        }

        float amount = event.getAmount();
        float health = event.getHealth();

        amount += health;
        float maxHealth = player.getMaxHealth();

        if (!(maxHealth >= amount)) {
            amount -= maxHealth;
            controller.heal(player, null, amount);
        }
    }
}
